package com.wm.wmim.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wm.wmim.common.dto.PageResult;
import com.wm.wmim.server.dao.ChatMessageRecordDao;
import com.wm.wmim.server.dto.bo.req.ListChatMessageRecordReqBO;
import com.wm.wmim.server.dto.bo.resp.ChatMessageRecordBO;
import com.wm.wmim.server.entity.ChatMessageRecordDO;
import com.wm.wmim.server.mapstruct.ChatMessageRecordMapstruct;
import com.wm.wmim.server.service.ChatMessageRecordService;
import com.wm.wmim.server.dbservice.ChatMessageRecordDbService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 消息记录表(ChatMessageRecord)表服务实现类
 *
 * @author weiming
 * @since 2024-09-02 22:59:15
 */
@Slf4j
@Service
public class ChatMessageRecordServiceImpl implements ChatMessageRecordService {

    @Resource
    private ChatMessageRecordDao chatMessageRecordDao;
    @Resource
    private ChatMessageRecordDbService chatMessageRecordDbService;

    @Resource
    private ChatMessageRecordMapstruct chatMessageRecordMapstruct;

    @Override
    public PageResult<ChatMessageRecordBO> listChatMessageRecord(ListChatMessageRecordReqBO reqBO) {
        log.info("[查询聊天记录] req={}",reqBO);
        PageInfo<ChatMessageRecordDO> pageInfo = PageHelper.startPage(reqBO.getPageNum(), reqBO.getPageSize())
                .doSelectPageInfo(() -> chatMessageRecordDbService.listMessageRecord(reqBO));
        if (pageInfo == null || CollectionUtils.isEmpty(pageInfo.getList())){
            return PageResult.fromPageInfo(PageInfo.emptyPageInfo());
        }
        List<ChatMessageRecordBO> recordBOList =  chatMessageRecordMapstruct.fromDOToBO(pageInfo.getList());
        return PageResult.fromPageInfo(pageInfo, recordBOList);
    }
}

